﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using HandyControl.Controls;
using Microsoft.Win32;
using WWLauncher.Constant;
using WWLauncher.Util;
using WWLauncher.ViewModel;

namespace WWLauncher.View.Other
{
    /// <summary>
    /// TextDialog.xaml 的交互逻辑
    /// </summary>
    public partial class IconInfoDialog
    {
        IconInfo Source { get; set; }
        private bool newIconInfo { get; set; }
        public IconInfoDialog()
        {
            newIconInfo = true;
            this.DataContext = new IconViewModel();
            Source = new IconInfo();

            InitializeComponent();
        }

        public IconInfoDialog(IconInfo sourceinfo)
        {
            this.DataContext = sourceinfo.To();
            Source = sourceinfo;
            InitializeComponent();
        }
        private void Border_Loaded(object sender, RoutedEventArgs e)
        {
            RunTimeStatus.WpfWindowClick = true;
            RunTimeStatus.WpfWindowOpening = true;

            BGSettingUtil.BGSetting(BGBorder);

            var data = this.DataContext as IconViewModel;
            data.ComboboxList = new ObservableCollection<ComplexInfoModel>();

            data.ComboboxList.Add(new ComplexInfoModel() { Key = Guid.Empty, Text = "                " });
            foreach (var item in MainViewModel.Instance().MenuList[MainViewModel.Instance().SelectedMenuIndex].IconList.Where(x => x.Id != Source.Id))
            {
                var newItem = new ComplexInfoModel() { Key = item.Id, Text = item.Name };
                if (data.LinkId == item.Id)
                {
                    data.ComboboxItem = newItem;
                }
                data.ComboboxList.Add(newItem);
            }

            if (data.LinkId == Guid.Empty)
            {
                data.ComboboxItem = data.ComboboxList[0];// 默认选中第一个
            }

        }
        /// <summary>
        /// 保存修改属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaveProperty_Click(object sender, RoutedEventArgs e)
        {
            Source.Name = IconName.Text.Trim();
            if (IconIsAdmin.IsChecked == true)
            {
                Source.StartType = IconStartType.AdminStartUp;
            }

            Source.BitmapImage = IconImg.Source as BitmapImage;
            Source.TargetPath = TbTargetPath.Text.Trim();
            Source.StartArg = StartArg.Text.Trim();
            Source.IsStartup = cbIsStartup.IsChecked.Value;
            Source.LinkId = (this.DataContext as IconViewModel).ComboboxItem.Key;
            if (newIconInfo)
            {
                MainViewModel.Instance().MenuList[MainViewModel.Instance().SelectedMenuIndex].IconList.Add(Source);
            }

            ConfigUtil.SaveAppData();

            this.Close();
        }

        /// <summary>
        /// 修改图标为默认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ReStoreImage_Click(object sender, RoutedEventArgs e)
        {
            var info = this.DataContext as IconViewModel;
            info.BitmapImage = FileIcon.GetBitmapImage(info.TargetPath);
        }

        /// <summary>
        /// 修改图标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EditImage_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog ofd = new OpenFileDialog
                {
                    Multiselect = false, //只允许选中单个文件
                    Filter = "所有文件(*.*)|*.*"
                };
                if (ofd.ShowDialog() == true)
                {
                    var info = this.DataContext as IconViewModel;
                    info.BitmapImage = ImageUtil.GetBitmapIconByPath(ofd.FileName);
                }
            }
            catch (Exception e1)
            {
                Growl.WarningGlobal("修改图标失败,已重置为默认图标!");
                LogUtil.WriteErrorLog(e1, "修改图标失败!");
            }

        }

        private void btnChoose_Click(object sender, RoutedEventArgs e)
        {
            var info = (IconViewModel)this.DataContext;
            OpenFileDialog ofd = new OpenFileDialog
            {
                InitialDirectory = @"D:\",
                Multiselect = false,
                Filter = "所有文件(*.*)|*.*"
            };
            if (ofd.ShowDialog() == true)
            {
                info.TargetPath = ofd.FileName;
                info.BitmapImage = ImageUtil.GetBitmapIconByPath(ofd.FileName);
                if (IconName.Text.Trim().IsEmpty())
                {
                    info.Name = Path.GetFileNameWithoutExtension(ofd.FileName);
                }
            }
        }
        /// <summary>
        /// 程序窗体拖拽
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DragMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DragMove();
            }
        }
        private void CloseBtn_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
 

        private void Window_Closing(object sender, CancelEventArgs e)
        {
            RunTimeStatus.WpfWindowClick = false;
            RunTimeStatus.WpfWindowOpening = false;
        }
    }
    public class ComplexInfoModel : INotifyPropertyChanged
    {
        private Guid key;
        /// <summary>
        /// Key值
        /// </summary>
        public Guid Key
        {
            get { return key; }
            set { key = value; OnPropertyChanged(nameof(Key)); }
        }

        private String text;


        /// <summary>
        /// Text值
        /// </summary>
        public String Text
        {
            get { return text; }
            set { text = value; OnPropertyChanged(nameof(Text)); ; }
        }
        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}
